package testcode;

public class ThreadCode implements Runnable
{
	/** what to print */
	String m_output;
	/** how many times to print it */
	int m_times;
	
	Object toSynchWith;
	
	public ThreadCode(String a_output, int a_times)
	{
		m_output = a_output;
		m_times = a_times;
	}
	public static void main(String[] args)
	{
		ThreadCode[] threadCodes = new ThreadCode[20];
		Integer semaphore = 1;
		for(int i = 0; i < threadCodes.length; ++i)
		{
			threadCodes[i] = new ThreadCode(""+(char)('a'+i), 5);
			threadCodes[i].toSynchWith = semaphore;
		}
		Thread[] t = new Thread[threadCodes.length];
		for(int i = 0; i < threadCodes.length; ++i)
		{
			t[i] = new Thread(threadCodes[i]);
		}
		for(int i = 0; i < t.length; ++i)
		{
			t[i].start();
		}
	}
	@Override
	public void run()
	{
		synchronized (toSynchWith)
		{
			for(int i = 0; i < m_times; ++i)
			{
				try{Thread.sleep(1);}catch(Exception e){}
				System.out.print(m_output);
			}
		}
	}
}
